<%--
  Created by IntelliJ IDEA.
  User: 70481
  Date: 2019/9/22
  Time: 9:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<link href="layui/css/layui.css" rel="stylesheet" type="text/css">

<head>
    <title>药品管理</title>
</head>
<body>
<table id="myTable" lay-filter = "myTable"></table>
<%--更新的表单--%>
<form class="layui-form layui-form-pane" action="" style="display: none" id="myForm" lay-filter="myForm">
    <input type="hidden" name="drugId" value="" id="drugId">
    <div class="layui-form-item">
        <div class="layui-inline" style="width: 385px">
            <label class="layui-form-label">药品名</label>
            <div class="layui-input-block">
                <input type="text" name="drugName" autocomplete="off" lay-verify="required" placeholder="药品名" class="layui-input">
            </div>
        </div>
        <div class="layui-inline" style="width: 385px">
            <label class="layui-form-label">规格</label>
            <div class="layui-input-block">
                <input type="text" name="drugSpec" autocomplete="off" lay-verify="required" placeholder="规格" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline" style="width: 385px">
            <label class="layui-form-label">单位</label>
            <div class="layui-input-block">
                <input type="text" name="drugUnit" lay-verify="required" placeholder="单位" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline" style="width: 385px">
            <label class="layui-form-label">生产企业</label>
            <div class="layui-input-block">
                <input type="text" name="drugProductCompany" lay-verify="required" placeholder="生产企业" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline" style="width: 385px">
            <label class="layui-form-label">生产日期</label>
            <div class="layui-input-block">
                <input type="text" name="drugProductDate" id="date1" lay-verify="required" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline" style="width: 385px">
            <label class="layui-form-label">保质期至</label>
            <div class="layui-input-block">
                <input type="text" name="drugOKDate" id="date2" lay-verify="required" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline" style="width: 385px">
            <label class="layui-form-label">进价</label>
            <div class="layui-input-block">
                <input type="text" name="drugImportPrice" lay-verify="required" placeholder="进价" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline" style="width: 385px">
            <label class="layui-form-label">零售价</label>
            <div class="layui-input-block">
                <input type="text" name="drugPrice" lay-verify="required" placeholder="零售价" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>



    <div class="layui-form-item" pane="">
        <div class="layui-inline" style="width: 385px">
            <label class="layui-form-label">销售状态</label>
            <div class="layui-input-inline">
                <input type="radio" name="drugSaleStatus" value="0" title="是" >
                <input type="radio" name="drugSaleStatus" value="1" title="否">
            </div>
        </div>

        <div class="layui-inline" style="width: 385px">
            <label class="layui-form-label">是否进口</label>
            <div class="layui-input-inline">
                <input type="radio" name="drugIsImport" value="0" title="是">
                <input type="radio" name="drugIsImport" value="1" title="否">
            </div>
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">产品说明 </label>
        <div class="layui-input-block">
            <textarea name="drugDescription" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="update">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

</body>
<script type="text/javascript" src="js/jquery-1.12.4.js"></script>
<script type="text/javascript" src="layui/layui.js"></script>

<script type="text/javascript">

    layui.use(["table","form","layer","laydate"],function () {
        var laydate = layui.laydate;
        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;

        laydate.render({
            elem: '#date1', //指定元素
            format: "yyyy-MM-dd"
        });

        laydate.render({
            elem: '#date2', //指定元素
            ormat: "yyyy-MM-dd"
        });
        // 初始化表格
        table.render({
            elem: '#myTable'
            ,url: '${pageContext.request.contextPath}/drug/selectDrug' //数据接口
            ,page: true //开启分页
            ,toolbar:"#toolHead"
            ,defaultToolbar:[]
            ,cols: [[ //表头
                {checkbox:"checkbox"},
                {field: 'drugId', title: '编号',width:100}
                ,{field: 'drugName', title: '药品名',width:120}
                ,{field: 'drugSpec', title: '规格',width:60}
                ,{field: 'drugUnit', title: '单位',width:60}
                ,{field: 'drugProductCompany', title: '生产企业'}
                ,{field: 'drugPrice', title: '销售价格',width:90}
                ,{field: 'drugSaleStatus', title: '销售状态',width:90,templet:"#titleTpl"}
                ,{toolbar:"#operation", title: '操作',align:'center'}
            ]]
        })

        // 头监听

        table.on("toolbar(myTable)",function (obj) {
            // alert(12334)
            switch (obj.event) {
                case "add":
                    // 添加操作 弹出表单 清空表单数据
                    $("#myForm")[0].reset();
                    layer.open({
                        type: 1,
                        offset: "t",
                        title:"药品信息入库",
                        anim: 1,
                        area:'800px',
                        content: $("#myForm")
                    });

                break;
                case "search":
                    var drugName = $("#drugName").val();
                    // 表格重载
                    table.reload("myTable",{
                        where:{"drugName":drugName}
                    });
                break;
            }
        });

        // 行监听
        table.on("tool(myTable)",function (obj) {
            //可以监听啦
/*            alert(obj.event)
            console.log(obj.data)*/
            var params = obj.data;
            switch(obj.event){
                case "del":
                    // 删除
                // alert(obj.data.drugId)
                    layer.confirm("确定删除该药品吗",{icon: 3, title:'提示'},function (index) {
                        $.ajax({
                            url:"${pageContext.request.contextPath}/drug/deleteByDrugId",
                            type:"post",
                            data:{"drugId":params.drugId},
                            success:function (data) {
                                layer.msg(data.isDelete,{time:1250});
                                table.reload("myTable");
                            }
                        });
                    });
                break;
                case "upd":
                    // 修改
                    // 表单数据回显
                    form.val("myForm",{
                        drugId:params.drugId,
                        drugName:params.drugName,
                        drugSpec:params.drugSpec,
                        drugUnit:params.drugUnit,
                        drugProductCompany:params.drugProductCompany,
                        drugProductDate:params.drugProductDate,
                        drugOKDate:params.drugOKDate,
                        drugImportPrice:params.drugImportPrice,
                        drugPrice:params.drugPrice,
                        drugSaleStatus:params.drugSaleStatus,
                        drugIsImport:params.drugIsImport,
                        drugDescription:params.drugDescription
                    });
                    // 弹出层显示数据
                    layer.open({
                        type:"1",
                        offset:"t",
                        title:"药品信息修改",
                        anim:"1",
                        area:"800px",
                        content:$("#myForm")
                    });
                break;
                case "sales":
                    // 下架
                    // 对药品的属性值进行判断
                    layer.confirm("确认更新药品状态吗",{icon: 3, title:'提示'},function (index) {
                        // alert(index)
                        var status = 0;
                        if(params.drugSaleStatus===0){
                            // 将状态修改为1
                            status=1;
                        }
                        $.ajax({
                            url:"${pageContext.request.contextPath}/drug/updateDrugSaleStatus",
                            type:"post",
                            data:{"drugId":params.drugId,"drugSaleStatus":status},
                            success:function(data){
                                layer.msg(data.isSales,{time:1250});
                                table.reload("myTable");
                            }
                        });
                    });
                break;
                case "examine":
                    // 审核 将药品信息添加到审核表
                    $.ajax({
                        url:"${pageContext.request.contextPath}/exam/insertExam",
                        type:"post",
                        data:{"drugId":params.drugId},
                        success:function(data){
                            layer.msg(data.isSuccess,{time:1250})
                        }
                    });
                break;
            }
        });


        form.on("submit(update)",function (data) {
            if(!$("#drugId").val()){
                $.ajax({
                    url:"${pageContext.request.contextPath}/drug/insertDrug",
                    type:"post",
                    data:data.field,
                    success:function (data) {
                        layer.msg(data.isInsert,{time:1250})
                        layer.closeAll("page");
                        table.reload("myTable");
                    }
                });
            }else{
                // 更新
                $.ajax({
                    url:"${pageContext.request.contextPath}/drug/updateDrug",
                    type:"post",
                    data:data.field,
                    success:function (data) {
                        layer.msg(data.isUpdate,{time:1250});
                        layer.closeAll("page");
                        table.reload("myTable")
                    }
                })
            }
            return false;
        })



    })
</script>

<script type="text/html" id="operation">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">
        <i class="layui-icon layui-icon-delete"></i>删除
    </a>
    <a class="layui-btn layui-btn-xs" lay-event="upd">
       <i class="layui-icon layui-icon-edit"></i> 修改
    </a>
    {{#  if(d.drugSaleStatus == 0){ }}
    <a class="layui-btn layui-btn-xs" lay-event="sales">
        <i class="layui-icon layui-icon-down"></i>停售
    </a>
    {{#  } else { }}
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="sales">
        <i class="layui-icon   layui-icon-up"></i>上架
    </a>
    {{#  } }}

    <a class="layui-btn  layui-btn-xs" lay-event="examine">
        <i class="layui-icon layui-icon-vercode"></i>提交审核
    </a>

</script>

<script type="text/html" id="toolHead">
    <a class="layui-btn layui-btn-radius layui-btn-normal layui-btn-xs" lay-event="add">
       <i class="layui-icon layui-icon-add-circle-fine"></i> 药品采购
    </a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="text" name="drugName" id="drugName" value="" placeholder="请输入药品关键字">
    <a class="layui-btn layui-btn-radius layui-btn-normal layui-btn-xs" lay-event="search">
        <i class="layui-icon layui-icon-search"></i>搜索
    </a>
</script>

<script type="text/html" id="titleTpl">
    {{#  if(d.drugSaleStatus == 0){ }}
    正常
    {{#  } else { }}
    停售
    {{#  } }}
</script>
</html>
